package bhbt.controller.member.kuang;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import bhbt.controller.core.BaseController;
import bhbt.model.Member;
import bhbt.model.ParamWage;
import bhbt.model.log.LogKuang;
import bhbt.service.DailiService;
import bhbt.service.MemberService;
import bhbt.service.ParamWageService;
import bhbt.service.log.LogKuangService;
import bhbt.utils.ContextUtil;
import bhbt.utils.CryptUtil;
import bhbt.utils.MapUtil;





@Controller
 
public class KuangPayController extends BaseController {

	@Resource
	private MemberService memberService;
	@Resource
	private LogKuangService logKuangService;
	 
	@Resource
	private ParamWageService paramWageService;

	@RequestMapping("member/kuang/kuangPayShow.shtml")
	public String show(HttpServletRequest request) throws Exception{
		
		//会员信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		request.setAttribute("member",member);
		
		//会员所有矿点
		Map<String, Object> mapLogKuang = new HashMap<String, Object>();
		mapLogKuang.put("memberId", member.getId());
		mapLogKuang.put("isActive", "F");
		List<LogKuang> logKuangNoActiveList = this.logKuangService.queryForList("select", mapLogKuang);
		request.setAttribute("logKuangNoActiveList",logKuangNoActiveList);
		
		request.setAttribute("num",logKuangNoActiveList.size());
		
		return "member/kuang/kuangPay";
	}
 
	@RequestMapping("member/kuang/kuangPay.shtml")
	public String ti(HttpServletRequest request, ModelMap model) throws Exception{
		
		Boolean result=true;
		//会员信息
		Member member = ContextUtil.getMember();
		member= memberService.getByKey(member.getId());
		 
		 
		Map formMap = getParameterMap(request);
		String twoPassword = MapUtil.getString(formMap, "twoPassword");
		String pwd = CryptUtil.getInstance().encrypt2String(twoPassword,CryptUtil.keyStr);
		String[] ids =MapUtil.getArray(formMap, "ids");
		
    	if(!member.getTwoPassword().equals(pwd)){
    		setMessage(model,"二级密码不正确。");
    		result=false;
    	}
    	
    	String content = MapUtil.getString(formMap, "content");
    	
    	//接收会员验证
		String loginName = MapUtil.getString(formMap, "loginName");
    	Member toMember = memberService.executMemberByLoginName(loginName);
    	if(toMember==null||toMember.getAccountActive().equals("F")){
    		setMessage(model,"会员编号不存在或者未激活。");
    		result=false;
    	}
    	
    	if(ids.length<=0){
    		setMessage(model,"请选择转让矿点。");
    		result=false;
    	}
    	
    	if(result){
    		setMessage(model,"矿点转让成功。");
    		this.logKuangService.saveKuangPay(ids,toMember,content);
    	}	
		request.setAttribute("member",member);
		//会员所有矿点
		Map<String, Object> mapLogKuang = new HashMap<String, Object>();
		mapLogKuang.put("memberId", member.getId());
		mapLogKuang.put("isActive", "F");
		List<LogKuang> logKuangNoActiveList = this.logKuangService.queryForList("select", mapLogKuang);
		request.setAttribute("logKuangNoActiveList",logKuangNoActiveList);
		
		request.setAttribute("num",logKuangNoActiveList.size());
		
		return "member/kuang/kuangPay";
    	
		
	}
	
	 
	
}
